package handler

import (
	"context"
	"google.golang.org/grpc/codes"
	"google.golang.org/grpc/status"
	"shop_srvs/userop_srv/global"
	"shop_srvs/userop_srv/model"
	"shop_srvs/userop_srv/proto"
)

func (UserOpServer) MessageList(ctx context.Context, req *proto.MessageRequest) (*proto.MessageListResponse, error) {
	//获取用户的购物车列表
	var Messagees []model.LeavingMessages
	var data []*proto.MessageResponse

	if result := global.DB.Where(&model.LeavingMessages{User: req.UserId}).Find(&Messagees); result.RowsAffected == 0 {
		return nil, status.Errorf(codes.NotFound, "没有留言信息")
	}
	for _, Message := range Messagees {
		data = append(data, &proto.MessageResponse{
			Id:          Message.ID,
			UserId:      Message.User,
			Message:     Message.Message,
			MessageType: Message.MessageType,
			Subject:     Message.Subject,
			File:        Message.File,
		})
	}
	return &proto.MessageListResponse{
		Total: int32(len(Messagees)),
		Data:  data,
	}, nil
}
func (UserOpServer) CreateMessage(ctx context.Context, req *proto.MessageRequest) (*proto.MessageResponse, error) {
	var message model.LeavingMessages

	message.User = req.UserId
	message.MessageType = req.MessageType
	message.Subject = req.Subject
	message.Message = req.Message
	message.File = req.File

	global.DB.Save(&message)

	return &proto.MessageResponse{Id: message.ID}, nil
}
